using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DM.Entity;
using DM.DAO;

namespace DM.Web
{
    public partial class Site : System.Web.UI.MasterPage
    {
        private CultureInfo ci;
        private ResourceManager rm;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["lang"] == null)
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
                Session["lang"] = "vi-VN";
            }
            else
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["lang"].ToString());
            }
            rm = new ResourceManager("Resources.Lang", Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            LoadString(ci);
            BindingFootter();
            BindingLeftNews();
            GenListTuyenDung();
            BindingDataListAd();
        }
        protected void ButtonVietNam_Click(object sender, EventArgs e)
        {
            Session["lang"] = "vi-VN";
            Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["lang"].ToString());
            LoadString(Thread.CurrentThread.CurrentCulture);
            Response.Redirect(Request.RawUrl);
            //Response.Redirect("TrangChu.aspx");
        }

        protected void ButtonEnglish_Click(object sender, EventArgs e)
        {
            Session["lang"] = "en-US";
            Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["lang"].ToString());
            LoadString(Thread.CurrentThread.CurrentCulture);
            Response.Redirect(Request.RawUrl);
            //Response.Redirect("TrangChu.aspx");
        }
        //Load ngôn ngữ
        public void LoadString(CultureInfo ci)
        {
            //Load menu text
            LabelTrangChu.Text = rm.GetString("TrangChu", ci);

            LabelGioiThieu.Text = rm.GetString("GioiThieu", ci);
            LabelTamNhin.Text = rm.GetString("TamNhin", ci);
            LabelVanHoaDoanhNghiep.Text = rm.GetString("VanHoaDoanhNghiep", ci);
            LabelCongNgheThietBi.Text = rm.GetString("CongNgheThietBi", ci);
            LabelMicromalting.Text = rm.GetString("Micromalting", ci);
            LabelHinhAnhCongTy.Text = rm.GetString("HinhAnhCongTy", ci);

            LabelSanPham.Text = rm.GetString("SanPham", ci);

            LabelQuanHeCoDong.Text = rm.GetString("QuanHeCoDong", ci);
            LabelDieuLeCongTy.Text = rm.GetString("DieuLeCongTy", ci);
            LabelThongTinTuHoiDongQuanTri.Text = rm.GetString("ThongTinTuHoiDongQuanTri", ci);
            LabelNghiQuyetHoiDongQuanTri.Text = rm.GetString("NghiQuyetHoiDongQuanTri", ci);
            LabelBaoCaoThuongNien.Text = rm.GetString("BaoCaoThuongNien", ci);
            LabelBCTCQuy.Text = rm.GetString("BCTCQuy", ci);
            LabelTinTuc.Text = rm.GetString("TinTuc", ci);

            LabelThongTinThiTruong.Text = rm.GetString("ThongTinThiTruong", ci);
            LabelThongTinKhachHang.Text = rm.GetString("ThongTinKhachHang", ci);
            LabelThongTinSanPham.Text = rm.GetString("ThongTinSanPham", ci);

            LabelDoiTac.Text = rm.GetString("DoiTac", ci);
            LabelKhachHang.Text = rm.GetString("KhachHang", ci);
            LabelNhaCungCap.Text = rm.GetString("NhaCungCap", ci);

            LabelCamKet.Text = rm.GetString("CamKet", ci);
            LabelAnToanLuongThuc.Text = rm.GetString("AnToanLuongThuc", ci);
            LabelAnToanLaoDong.Text = rm.GetString("AnToanLaoDong", ci);
            LabelDamBaoMoiTruong.Text = rm.GetString("DamBaoMoiTruong", ci);
            LabelTuThienXaHoi.Text = rm.GetString("TuThienXaHoi", ci);

            LabelLienHe.Text = rm.GetString("LienHe", ci);

            lbTinTuyenDung.Text = rm.GetString("TinTuyenDung", ci);
            lbTiGiaVang.Text = rm.GetString("TyGiaVang", ci);

            lbVerTrangChu.Text = rm.GetString("TrangChu", ci);
            lbVerTinTuc.Text = rm.GetString("TinTuc", ci);
            lbVerSanPham.Text = rm.GetString("SanPham", ci);
            lbVerQuanHeCoDong.Text = rm.GetString("QuanHeCoDong", ci);
            lbVerLienHe.Text = rm.GetString("LienHe", ci);
            lbVerGioiThieu.Text = rm.GetString("GioiThieu", ci);
            lbVerDoiTac.Text = rm.GetString("DoiTac", ci);
            lbVerCamKet.Text = rm.GetString("CamKet", ci);

            lbHoatDongVanHoa.Text = rm.GetString("HoatDongVanHoa", ci);
            lbNoiBo.Text = rm.GetString("NoiBo", ci);
        }
        public void BindingFootter()
        {
            int lang = Session["lang"].ToString() == "vi-VN" ? 1 : 2;
            TinTucDAO _tinTucDao = new TinTucDAO();
            TinTuc tinTuc = _tinTucDao.Gets(Common.TinThongTinLienHe, 1, lang)[0];
            lbFootter.Text = tinTuc.Noidung;
        }

        /// <summary>
        /// Lấy ra 2 tin giới thiệu bất kỳ đưa ra màn hình trong phần sitebar bên trái
        /// </summary>
        private void BindingLeftNews()
        {
            int lang = Session["lang"].ToString() == "vi-VN" ? 1 : 2;
            TinTucDAO _tintucDao = new TinTucDAO();
            HinhDAO _hinhDao = new HinhDAO();
            List<TinTuc> listTinGT = _tintucDao.GetTinTucRandom(lang);
            //lấy ra màn hình tin tức thú nhât
            lbTitle1.Text = listTinGT[0].Tieude;
            lbTitle2.Text = listTinGT[1].Tieude;
            lbContent1.Text = listTinGT[0].Mota;
            lbContent2.Text = listTinGT[1].Mota;
            link1.HRef = "TrangTinTuc.aspx?id=" + listTinGT[0].Ma;
            link2.HRef = "TrangTinTuc.aspx?id=" + listTinGT[1].Ma;
            Hinh hinh1 = _hinhDao.Get(listTinGT[0]);
            Hinh hinh2 = _hinhDao.Get(listTinGT[1]);
            imgNew1.ImageUrl = "../" + hinh1.Hinhurl;
            imgNew2.ImageUrl = "../" + hinh2.Hinhurl;
        }

        private void GenListTuyenDung()
        {
            int lang = Session["lang"].ToString() == "vi-VN" ? 1 : 2;
            TinTucDAO _tinDao = new TinTucDAO();
            List<TinTuc> list = _tinDao.Gets(51, 3, lang);
            if (list[0].Ma != 0)
            {
                lbTinTuyenDung.Visible = true;
                listTuyenDung.InnerHtml += "<ul>";

                foreach (TinTuc tinTuc in list)
                {
                    listTuyenDung.InnerHtml += "<li>";
                    listTuyenDung.InnerHtml += "<a href=\"../TrangTinTuc.aspx?id=" + tinTuc.Ma + "\">" + tinTuc.Tieude + "</a>";
                    listTuyenDung.InnerHtml += "</li>";
                }
                listTuyenDung.InnerHtml += "</ul>";
            }
            else
            {
                lbTinTuyenDung.Visible = false;
                tuyenDung.Visible = false;
            }
        }

        protected void DataListAdImage_OnItemDataBound(object sender, DataListItemEventArgs e)
        {
            Label fileExt = (Label)e.Item.FindControl("LabelFileExtention");
            if (fileExt.Text == "swf")
            {
                Panel panelImage = (Panel)e.Item.FindControl("image");
                panelImage.Visible = false;
            }
            else
            {
                Panel panelFlash = (Panel)e.Item.FindControl("flashAd");
                panelFlash.Visible = false;
            }
        }
        
        private void BindingDataListAd()
        {
            FileInfo xmlFile = new FileInfo(MapPath("Photos/QuangCao/catalogimage.xml"));
            if (xmlFile.Exists)
            {
                DataSet dataSet = new DataSet();
                dataSet.ReadXml(MapPath("Photos/QuangCao/catalogimage.xml"));
                if (dataSet.Tables.Count != 0)
                {
                    dataSet.Tables[0].DefaultView.Sort = "Name";
                    DataView dataView = dataSet.Tables[0].DefaultView;
                    DataListAdImage.DataSource = dataView;
                    DataListAdImage.DataBind();
                }
                else
                {
                    DataListAdImage.Enabled = false;
                }
            }
            else
            {
                DataListAdImage.Enabled = false;
            }
        }
    }
}
